package com.elm_springboot_s.controller;

import com.elm_springboot_s.pojo.DeliveryAddress;
import com.elm_springboot_s.pojo.RespondMessage;
import com.elm_springboot_s.service.DeliveryAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/deliveryAddress")
@Api(tags = "配送地址相关接口")
public class DeliveryAddressController {

    @Autowired
    private DeliveryAddressService deliveryAddressService;

    @GetMapping("/listDeliveryAddressByUserId")
    @ApiOperation("根据用户编号查询所属送货地址")
    public RespondMessage<List<DeliveryAddress>> listDeliveryAddressByUserId(Long userId) {
        try {
            List<DeliveryAddress> addresses = deliveryAddressService.listDeliveryAddressByUserId(userId);
            return RespondMessage.success(addresses);
        } catch (Exception e) {
            return RespondMessage.error("查询送货地址失败：" + e.getMessage());
        }
    }

    @GetMapping("/getDeliveryAddressById")
    @ApiOperation("根据送货地址编号查询送货地址")
    public RespondMessage<DeliveryAddress> getDeliveryAddressById(Integer daId) {
        try {
            DeliveryAddress address = deliveryAddressService.getDeliveryAddressById(daId);
            return RespondMessage.success(address);
        } catch (Exception e) {
            return RespondMessage.error("查询送货地址失败：" + e.getMessage());
        }
    }

    @PostMapping("/saveDeliveryAddress")
    @ApiOperation("添加送货地址")
    public RespondMessage<Integer> saveDeliveryAddress(String contactName, Integer contactSex,
                                                     String contactTel, String address, Long userId) {
        try {
            int result = deliveryAddressService.saveDeliveryAddress(contactName, contactSex, 
                                                                  contactTel, address, userId);
            return RespondMessage.success(result);
        } catch (Exception e) {
            return RespondMessage.error("添加送货地址失败：" + e.getMessage());
        }
    }

    @PutMapping("/updateDeliveryAddress")
    @ApiOperation("更新送货地址")
    public RespondMessage<Integer> updateDeliveryAddress(Integer daId, String contactName, 
                                                       Integer contactSex, String contactTel, 
                                                       String address, Long userId) {
        try {
            int result = deliveryAddressService.updateDeliveryAddress(daId, contactName, contactSex, 
                                                                    contactTel, address, userId);
            return RespondMessage.success(result);
        } catch (Exception e) {
            return RespondMessage.error("更新送货地址失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/removeDeliveryAddress")
    @ApiOperation("删除送货地址")
    public RespondMessage<Integer> removeDeliveryAddress(Integer daId) {
        try {
            int result = deliveryAddressService.removeDeliveryAddress(daId);
            return RespondMessage.success(result);
        } catch (Exception e) {
            return RespondMessage.error("删除送货地址失败：" + e.getMessage());
        }
    }
} 